package com.example.demo.web;

import com.example.demo.entity.User;
import com.example.demo.param.dto.user.*;
import com.example.demo.service.IUserService;
import com.example.demo.util.Json;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "用户")
@RestController
@RequestMapping("user")
public class UserController extends BaseController{

    @Autowired private IUserService userService;

    @ApiOperation("用户注册")
    @PostMapping("reg")
    public Json<Boolean> userReg(@RequestBody RegDto dto){
        return new Json<>(this.userService.userReg(dto.getUsername(),dto.getPassword(),0));
    }

    @ApiOperation("添加管理员")
    @PostMapping("reg/admin")
    public Json<Boolean> adminReg(@RequestBody AddAdminDto dto){
        return new Json<>(this.userService.userReg(dto.getUsername(),dto.getPassword(),2));
    }

    @ApiOperation("修改管理员密码")
    @PutMapping("put/admin")
    public Json<Boolean> adminPut(@RequestBody UpdateAdminDto dto){
        return new Json<>(this.userService.updateAdminPassword(dto));
    }

    @ApiOperation("删除管理员")
    @DeleteMapping("dalete/admin")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "query", dataType = "String")
    })
    public Json<Boolean> deleteAdmin(@RequestParam String id, @RequestParam String token){
        return new Json<>(this.userService.deleteAdmin(token, id));
    }

    @ApiOperation("用户登录")
    @PostMapping("login")
    public Json<User> userLogin(@RequestBody LoginDto dto){
        return new Json<>(this.userService.userLog(dto.getUsername(),dto.getPassword(),0));
    }

    @ApiOperation("管理员登录")
    @PostMapping("login/admin")
    public Json<User> adminLogin(@RequestBody LoginDto dto){
        return new Json<>(this.userService.userLog(dto.getUsername(),dto.getPassword(),2));
    }

    @ApiOperation("修改个人信息")
    @PutMapping("updateImformation")
    public Json<Boolean> updateImformation(@RequestBody UpdateUserDto dto){
        String uid = this.verification(dto.getToken());
        Boolean exit = this.userService.updateImformation(uid,dto.getUsername(),dto.getGender(),dto.getBirthday(),dto.getProduce());
        Json<Boolean> json = new Json<>(exit);
        return json;
    }

    @ApiOperation("修改密码")
    @PutMapping("updatePassword")
    public Json<Boolean> updatePassword(@RequestBody UpdatePasswordDto dto){
        String uid = this.verification(dto.getToken());
        Boolean exit = this.userService.updatePassword(uid,dto.getOldPassword(),dto.getNewPassword());
        Json<Boolean> json = new Json<>(exit);
        return json;
    }
}
